import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:gold/base/provider_scaffold.dart';
import 'package:gold/ui/my/fund/fund_details_viewmodel.dart';
import 'package:gold/utils/res.dart';
import 'package:gold/utils/xfg_color.dart';
import 'package:gold/utils/xfg_size.dart';
import 'package:gold/widgets/common_widgets.dart';
import 'package:gold/widgets/xfg_text.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';

/// @description: 资金明细
/// @author tangzhentao
/// @created at 22:56 2024/12/14

class FundDetailsPage extends StatelessWidget {
  const FundDetailsPage({super.key});

  @override
  Widget build(BuildContext context) {
    return ProviderScaffold<FundDetailsViewmodel>(
      model: FundDetailsViewmodel(),
      appBar: (_) => xfgAppbar(title: '资金明细', backgroundColor: XfgColor.transparent),
      onInit: (data) => data.loadFirst(),
      toplessBuilder: (_, fund) => Container(
        padding: EdgeInsets.fromLTRB(XfgSize().size10, XfgSize().size20, XfgSize().size10, XfgSize().size10),
        child: Builder(builder: (ctx) {
          final fundType = ctx.select<FundDetailsViewmodel, FundType>((k) => k.fundType);
          return Row(
            children: [
              _typeWidget(FundType.all, fundType, fund),
              SizedBox(width: XfgSize().size10),
              _typeWidget(FundType.income, fundType, fund),
              SizedBox(width: XfgSize().size10),
              _typeWidget(FundType.expend, fundType, fund),
            ],
          );
        }),
      ),
      builder: (ctx, fund) => SmartRefresher(
        controller: fund.controller,
        enablePullUp: true,
        enablePullDown: true,
        onRefresh: fund.refresh,
        onLoading: fund.loadMore,
        child: ListView.builder(
          itemCount: fund.list.length,
          itemBuilder: (_, index) {
            final model = fund.list[index];
            final isLast = index == fund.list.length - 1;
            return GestureDetector(
              child: Container(
                margin: EdgeInsets.fromLTRB(XfgSize().size10, 0, XfgSize().size10, isLast && XfgSize().bottomBarHeight != 0 ? XfgSize().bottomBarHeight:XfgSize().size10),
                decoration: white6Decoration,
                child: Column(
                  children: [
                    GestureDetector(
                      onTap: () => fund.changeShowDetails(index),
                      child: Container(
                        height: XfgSize().size48 + (model.showDetails ? XfgSize().size0_5:0),
                        padding: EdgeInsets.symmetric(horizontal: XfgSize().size12),
                        decoration: model.showDetails ? bottomBorderDecoration:null,
                        child: Row(
                          children: [
                            XfgText(model.date, fontSize: XfgSize().text18, color: XfgColor.text1D, bold: FontWeight.bold),
                            SizedBox(width: XfgSize().size16),
                            Image.asset(model.showDetails ? Res.imagesTriangleUp:Res.imagesTriangleDown, width: XfgSize().size18, height: XfgSize().size18)
                          ],
                        ),
                      ),
                    ),
                    Visibility(
                      visible: model.showDetails,
                      child: Column(
                        children: model.data.map((fundModel) {
                          final index = model.data.indexOf(fundModel);
                          final isAdd = (fundModel.amount?? 0) > 0;
                          String desc = '';
                          if (fundModel.withdrawal != null) {
                            if (fundModel.withdrawal!.status == 2) {
                              desc = '已到账';
                            } else if (fundModel.withdrawal!.status == 3) {
                              desc = '审核失败';
                            } else {
                              desc = '审核中';
                            }
                          } else {
                            desc = '收入金额 ${fundModel.currentAmount?? 0}';
                          }
                          return Padding(
                            padding: EdgeInsets.fromLTRB(XfgSize().size12, XfgSize().size15, XfgSize().size12, index == model.data.length - 1 ? XfgSize().size25:XfgSize().size15),
                            child: Column(
                              children: [
                                Row(
                                  children: [
                                    Expanded(child: XfgText(fundModel.reason?? '', fontSize: XfgSize().text16, color: XfgColor.text1, maxLines: 1, overflow: TextOverflow.ellipsis)),
                                    SizedBox(
                                      width: XfgSize().size92,
                                      child: XfgText('${isAdd ? '+':'-'}${fundModel.amount?.abs()?? 0}', fontSize: XfgSize().text16, color: isAdd ? XfgColor.integralPlus:XfgColor.text1, bold: FontWeight.bold, textAlign: TextAlign.end),
                                    )
                                  ],
                                ),
                                SizedBox(height: XfgSize().size10),
                                Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                  children: [
                                    XfgText(fundModel.showTime?? '', fontSize: XfgSize().text14, color: XfgColor.text3),
                                    XfgText(desc, fontSize: XfgSize().text14, color: XfgColor.text3)
                                  ],
                                )
                              ],
                            ),
                          );
                        }).toList(),
                      ),
                    )
                  ],
                ),
              ),
            );
          },
        ),
      ),
    );
  }

  /// 资金类型布局
  Widget _typeWidget(FundType type, FundType select, FundDetailsViewmodel fund) => GestureDetector(
    onTap: () {
      if (select != type) {
        fund.fundType = type;
      }
    },
    child: Container(
      width: XfgSize().size60,
      height: XfgSize().size33,
      decoration: BoxDecoration(
        color: XfgColor.white,
        border: select == type ? Border.all(color: XfgColor.theme, width: XfgSize().size0_5):null,
        borderRadius: BorderRadius.circular(XfgSize().size33)
      ),
      alignment: Alignment.center,
      child: XfgText(type.des, fontSize: XfgSize().text14, color: select == type ? XfgColor.text1D:XfgColor.text2),
    ),
  );
}
